/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.workflow.logic.operations;

import java.lang.reflect.Method;
import java.util.Vector;

import org.activequant.workflow.logic.annotations.Process;
import org.apache.log4j.Logger;



/**
 * An OperationBase subclass. Holds the following associated variables:
 * <ul>
 * <li>methods(Method[][])</li>
 * <li>services(Object[])</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [30.04.2007] Created (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 */
public class Split extends OperationBase {
	
	protected final static Logger log = Logger.getLogger(Split.class);
	/**
	 * private Method[][] methods;
	 */
	private Method[][] methods;
	/**
	 * private Object[] services;
	 */
	private Object[] services;
	/**
	 * constructs a Split(OperationBase subclass) using the given services(Object[]) to set its associated services(Object[]).<br/>
	 * Sets its associated methods(Method[][]) with Process annotated methods declared in the Class of each Object in the given services(Object[]).
	 * @param services
	 */
	public Split(Object[] services) {
		this.services = services;
		this.methods = new Method[services.length][];
		for(int i = 0; i < services.length; i++) {
			this.methods[i] = getMethodsByAnnotation(services[i]);
		}		
	}
	/**
	 * returns a Method[] holding all Process annotated methods declared(and inherited) in the Class of the given target(Object)
	 * @param target
	 * @return
	 */
	private Method[] getMethodsByAnnotation(Object target) {
		return getMethodsByAnnotation(target.getClass(), Process.class);
	}
	/**
	 * for each service(Object) in the associated services(Object[]), finds a Method whose signature takes no arguments and invokes it. Returns a new Result object.
	 */
	public Result execute() throws Exception {
		return execute(new Object[]{});
	}
	/**
	 * for each service(Object) in the associated services(Object[]), finds a Method whose signature accepts the given args(Object[]) and invokes it passing in the given
	 * args(Object[]). Returns a new Result object.
	 */
	public Result execute(Object arg) throws Exception {
		return execute(new Object[]{arg});
	}
	/**
	 * for each service(Object) in the associated services(Object[]), finds a Method whose signature accepts the given args(Object[]) and invokes it passing in the given
	 * args(Object[]). Returns a new Result object.
	 * @param args
	 * @return
	 * @throws Exception
	 */
	private Result execute(Object[] args) throws Exception {
		for (int i = 0; i < services.length; i++) {
			Method method = getMethodBySignature(methods[i], args);
			log.info("Invoking method '" + method.getName()
					+ "' in service class '"
					+ services[i].getClass().getSimpleName() + "'.");
			method.invoke(services[i], args);
		}
		return new Result();
	}
	/**
	 * for each service(Object) in the associated services(Object[]), finds a Method whose return type matches the given signature(Class&lt;?&gt;) and invokes it without arguments. 
	 * Returns a new Result object.
	 */
	public Result execute(Class<?> signature) throws Exception {
		for (int i = 0; i < services.length; i++) {
			Method method = getMethodByReturnType(methods[i], signature);
			log.info("Invoking method '" + method.getName()
					+ "' in service class '"
					+ services[i].getClass().getSimpleName() + "'.");
			method.invoke(services[i], new Object[]{});
		}
		return new Result();
	}
	/**
	 * returns a Method[] holding all the Methods from the associated methods(Method[][])
	 */
	public Method[] getMethods() {
		Vector<Method> allMethods = new Vector<Method>();
		for(Method[] methodArray: methods) {
			for(Method method: methodArray) {
				allMethods.add(method);
			}
		}
		return allMethods.toArray(new Method[]{});
	}
}